package com.uatair.common.mqtt.uploader;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.uatair.common.mqtt.domain.Content;
import com.uatair.common.mqtt.domain.OsdData;
import com.uatair.common.mqtt.util.AESUtil;
import com.uatair.common.mqtt.util.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class NanHangTaskUploader {

    private static final Logger log = LoggerFactory.getLogger(NanHangTaskUploader.class);

    private static final String nanhangUpDataUrl = "http://cloud.tcuam.com:18120/prod-api/msg/upMsgData"; // 南航数据上报URL
    private static final String aesKey = "c82f14b2f3a611ecbc840242ac110013";

    public void sendData2Nanhang(Content content, OsdData uavInfo, String nanhangSN) {
        try {
            //2 send data by http
            long time = System.currentTimeMillis();
            Map<String, Object> head = new HashMap<>();
            head.put("msgId", 50006);//消息号，按要求传输
            head.put("msgNo", time / 1000);//消息序号，发送方序号依次递增，接收方响应序号与请求序号相同，
            head.put("res", 4);//消息来源，1：机载设备，2：地面站，3：无人机云系统，4：企业云平台，
            head.put("des", 4);//消息目标，1：机载设备，2：地面站，3：无人机云系统，4：企业云平台，
            head.put("timestamp", time);//时间戳，单位ms


            JSONObject payload = new JSONObject();
            payload.put("regno", nanhangSN);
            payload.put("SNCode", "SUATQ20B"+content.getGateway());
            payload.put("orderId", uavInfo.getOrder_id());
            payload.put("time", uavInfo.getTimestamp());
            payload.put("alt", uavInfo.getAltitude());
            payload.put("lng", uavInfo.getLongitude());
            payload.put("lat", uavInfo.getLatitude());
            payload.put("head", uavInfo.getHeading());//真航向
            //not must
            payload.put("ht", uavInfo.getHeight());// 相对高度
            payload.put("height", uavInfo.getHeight());//星基高度
            payload.put("pit", uavInfo.getPitch());//俯仰角
            payload.put("roll", uavInfo.getRoll());//横滚角
//            payload.put("bat", uavInfo.getBattaryRemain());//电池容量百分比
//            payload.put("vol", uavInfo.getVoltage());//电池电压
//            payload.put("fucl", uavInfo.getf());//燃油剩余量百分比，或燃油数量升
//            payload.put("act", uavInfo.get());//活动类型
//            payload.put("mode", uavInfo.getFlightMode());
//            payload.put("arm", uavInfo.getArmd());//马达是否启动
//            payload.put("air", uavInfo.());//是否起飞到空中
            payload.put("absp", Math.abs(uavInfo.getGs()));//当前绝对速度
//            payload.put("temp", uavInfo.getImuTemp());//环境温度
//            payload.put("tc", uavInfo.());//当前故障码
            String payloadStr = payload.toJSONString();
            String payloadMask = AESUtil.AESEncode(payloadStr, aesKey);

            JSONObject req = new JSONObject();
            req.put("head", head);
            req.put("payload", payloadMask);
            log.info("sendData2Nanhang调用数据: url = {}, head = {}, req = {},payloadStr = {}", nanhangUpDataUrl, head, req, payloadStr);
            String res = HttpClientUtil.doPostJson(nanhangUpDataUrl, req.toJSONString());
//            log.info("sendData2Nanhang返回数据: res = {}", res);
            if (StringUtils.isBlank(res) || JSON.parseObject(res).getInteger("code") != 200) {
                log.error("sendData2Nanhang error. e = {}", res);
            }

        } catch (Exception e) {
            log.error("sendData2Nanhang: e = ", e);
        }

    }
}

